package org.rambadger.command.representation;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;
import org.mongojack.Id;
import org.mongojack.MongoCollection;
import org.rambadger.command.validation.UrlIdentifier;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@MongoCollection(name = "tracks")
public class Track {
    public static class TrackId {
        @UrlIdentifier @NotNull
        @JsonProperty String platformId;

        @UrlIdentifier @NotNull
        @JsonProperty String trackId;

        TrackId() {}

        public TrackId(String platformId, String trackId) {
            this.platformId = platformId;
            this.trackId = trackId;
        }

        public String getPlatformId() {
            return platformId;
        }

        public String getTrackId() {
            return trackId;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (!(o instanceof TrackId)) return false;

            TrackId trackId1 = (TrackId) o;

            if (!platformId.equals(trackId1.platformId)) return false;
            if (!trackId.equals(trackId1.trackId)) return false;

            return true;
        }

        @Override
        public int hashCode() {
            int result = platformId.hashCode();
            result = 31 * result + trackId.hashCode();
            return result;
        }
    }

    @Id @NotNull
    @JsonProperty TrackId trackId;

    @Length(max = 64) @NotBlank
    @JsonProperty String trackName;

    @Length(max = 4096) @NotBlank
    @JsonProperty String description;

    @UrlIdentifier
    @JsonProperty List<String> assignments;

    Track() {}

    public Track(String platformId, String trackId, String trackName, String description) {
        this.trackId = new TrackId(platformId, trackId);
        this.trackName = trackName;
        this.description = description;
        this.assignments = new LinkedList<>();
    }

    public TrackId getTrackId() {
        return trackId;
    }

    public String getTrackName() {
        return trackName;
    }

    public void setTrackName(String trackName) {
        this.trackName = trackName;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public List<String> getAssignments() {
        return assignments;
    }

    public void setAssignments(List<String> assignments) {
        this.assignments = assignments;
    }

    public void addAssignment(int index, String assignment){
        this.assignments.add(index, assignment);
    }

    public void addAssignment(String assignment){
        this.assignments.add(assignment);
    }

    public void removeAssignment(String assignment){
        this.assignments.remove(assignment);
    }

    public void moveAssignment(String assignment, int newIndex){
        int index = this.assignments.indexOf(assignment);
        this.assignments.remove(index);
        this.assignments.add(newIndex, assignment);

    }
}
